package com.gxa.ehome.controller;

import com.gxa.ehome.entity.PageBean;
import com.gxa.ehome.pojo.SysUser;
import com.gxa.ehome.service.SysUserservice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

//@Controller
//@ResponseBody
@RestController
@RequestMapping("/sysUser")
@Api("执行系统用户操作的控制器")
public class SysUserController {

    @Resource(name = "sysUserService")
    private SysUserservice sysUserservice;


    //    分页：前端传递的参数：page表示页码 limit表示每页的记录数
    @RequestMapping("/selectByPage")
    //ResponseBody:把下面所有要返回的数据转为JSON
    @ResponseBody
    public Map<String, Object> selectByPage(@RequestParam("page")int page, @RequestParam("limit")int limit){
        System.out.println("page"+page);
        System.out.println("limit"+limit);

        PageBean<SysUser> pageBean = sysUserservice.selectByPage(page, limit);

        Map<String,Object> jsonMap = new Hashtable<>();
        jsonMap.put("data",pageBean.getDataList());
        jsonMap.put("code",0);
        jsonMap.put("msg","用户data");
        jsonMap.put("count",pageBean.getCount());
        return jsonMap;

    }

    /**
     * 增删改查
     * GET 方式请求 /api/user/ 返回用户列表
     * GET 方式请求 /api/user/1返回id为1的用户
     * POST 方式请求 /api/user/ 通过user对象的JSON 参数创建新的user对象
     * PUT 方式请求 /api/user/3 更新id为3的发送json格式的用户对象
     * DELETE 方式请求/api/user/4删除 ID为 4的user对象
     * DELETE 方式请求/api/user/删除所有user
     */

    //这两种写法效果相同
//    @RequestMapping(method = RequestMethod.POST)
//    public void insert(){
//    }
//
//    @RequestMapping()
//    @DeleteMapping
//    public void delete(){
//    }

    @GetMapping()
    @ApiOperation(value = "查询全部系统用户的方法",notes="该方法通过restful中的Get方式访问，没有参数.")
    public List<SysUser> selectAll(){ //http://localhost:8080/ehome/sysUser
        System.out.println("查询所有系统用户");
        PageBean<SysUser> pageBean = sysUserservice.selectByPage(1, 5);
        return pageBean.getDataList();
    }

    @GetMapping("/{id}")
    public List<SysUser> selectSysUserById(@PathVariable("id") Integer id){ //http://localhost:8080/ehome/sysUser
        System.out.println("根据id查询系统用户:"+id);
        return null;
    }

    @PostMapping
    public void insert(@RequestBody SysUser sysUser){
        System.out.println("添加系统用户");
        System.out.println(sysUser);
    }

    @PutMapping("/{id}")
    public void update(@PathVariable("id") Integer id,@RequestBody SysUser sysUser){
        System.out.println("修改系统用户");
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") Integer id){
        System.out.println("根据id删除用户记录，目标id为："+id);
    }

    @DeleteMapping
    public void deleteAll(){
        System.out.println("全部删除");
    }

}
